package com.qdu.controller;


import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qdu.entity.Acadamy;
import com.qdu.entity.Batch;
import com.qdu.entity.Building;
import com.qdu.entity.Classroom;
import com.qdu.entity.Course;
import com.qdu.entity.CoursePlan;
import com.qdu.entity.LessionPlan;
import com.qdu.entity.Major;
import com.qdu.entity.Role;
import com.qdu.entity.Structure;
import com.qdu.entity.Student;
import com.qdu.entity.Teacher;
import com.qdu.entity.Term;
import com.qdu.entity.Users;
import com.qdu.service.AcadamyService;
import com.qdu.service.BatchService;
import com.qdu.service.BuildingService;
import com.qdu.service.ClassroomService;
import com.qdu.service.CoursePlanService;
import com.qdu.service.CourseService;
import com.qdu.service.CurrentTermService;
import com.qdu.service.LessionPlanService;
import com.qdu.service.MajorService;
import com.qdu.service.StructureService;
import com.qdu.service.StudentService;
import com.qdu.service.TeacherService;
import com.qdu.service.TermService;
import com.qdu.service.UsersService;

@Controller
@RequestMapping("/mc")
public class ManagerController {
	@Autowired
	AcadamyService acadamyService;
	@Autowired
	MajorService majorService;
	@Autowired
	CourseService courseService;
	@Autowired
	BuildingService buildingService;
	@Autowired
	BatchService batchService;
	@Autowired
	TermService termService;
	@Autowired
	ClassroomService classroomService;
	@Autowired
	LessionPlanService lessionPlanService;
	@Autowired
	StudentService studentService;
	@Autowired
	StructureService structureService;
	@Autowired
	TeacherService teacherService;
	@Autowired
	CurrentTermService currentTermService;
	@Autowired
	CoursePlanService coursePlanService;
	@Autowired
	UsersService usersSerivce;
	@Autowired
	PasswordEncoder pwdEncoder;
	//getall
	@GetMapping("/getAllAcadamy")
	public String getAllAcadamy(@RequestParam(defaultValue = "1") int pageNo, Model model) {
		PageHelper.startPage(pageNo, 6);
		List<Acadamy> acadamylist = acadamyService.selectAllAcadamy();
		PageInfo<Acadamy> pageInfo = new PageInfo<>(acadamylist, 5);
		model.addAttribute("acadamylist", acadamylist);
		model.addAttribute("pageInfo", pageInfo);
		return "Manager/AcadamyManage";
	}
	@RequestMapping("/getAllMajor")
	public String getAllMajor(@RequestParam(defaultValue = "1") int pageNo, Model model){
		model.addAttribute("acadamylist", acadamyService.selectAllAcadamy());
	
		List<Term> termlist = termService.selectAllTerm();
		List<Term> termlist2 = new ArrayList<>();
		for(Term t:termlist) {
			String tname = t.getTermName();
			if(tname.contains("秋")) {
				termlist2.add(t);
			}
		}
		model.addAttribute("termlist2", termlist2);
		PageHelper.startPage(pageNo, 6);
		List<Major> majorList = majorService.selectAllMajor();
		PageInfo<Major> pageInfo = new PageInfo<>(majorList, 5);
		model.addAttribute("majorlist",majorList);
		model.addAttribute("pageInfo", pageInfo);
		return "Manager/MajorManage";
	}
	
	@RequestMapping("/getAllBuilding")
	public String getAllBuilding(@RequestParam(defaultValue = "1") int pageNo,Model model) {
		PageHelper.startPage(pageNo, 7);
		List<Building> list = buildingService.selectAllBuilding();
		PageInfo<Building> pageInfo = new PageInfo<>(list, 5);
		model.addAttribute("buildinglist", list);
		model.addAttribute("pageInfo", pageInfo);
		return "Manager/BuildingManage";
	}
	@RequestMapping("/getAllBatch")
	public String getAllBatch(@RequestParam(defaultValue = "1") int pageNo,Model model) {
		PageHelper.startPage(pageNo, 10);
		List<Batch> batchlist = batchService.selectAllBatch();
		PageInfo<Batch> pageInfo = new PageInfo<>(batchlist, 10);
		model.addAttribute("acadamylist", acadamyService.selectAllAcadamy());
		model.addAttribute("batchlist", batchlist);
		model.addAttribute("majorlist", majorService.selectAllMajor());
		model.addAttribute("pageInfo", pageInfo);
		return "Manager/BatchManage";
	}
	@RequestMapping("/getAllClass")
	public String getAllClass(@RequestParam(defaultValue = "1") int pageNo,Model model) {
		PageHelper.startPage(pageNo, 10);
		List<Classroom> classrmlist = classroomService.selectAllClassroom();
		PageInfo<Classroom> pageInfo = new PageInfo<>(classrmlist, 10);
		model.addAttribute("classrmlist", classrmlist);
		model.addAttribute("buildinglist", buildingService.selectAllBuilding());
		model.addAttribute("pageInfo", pageInfo);
		return "Manager/ClassrmManage";
	}
	@RequestMapping("/getAllTerm")
	public String getAllTerm(@RequestParam(defaultValue = "1") int pageNo,Model model) {
		PageHelper.startPage(pageNo, 10);
		List<Term> termlist = termService.selectAllTerm();
		PageInfo<Term> pageInfo = new PageInfo<>(termlist, 10);
		model.addAttribute("currentterm", currentTermService.getAllCurrentTerm());
		model.addAttribute("termlist", termlist);
		model.addAttribute("pageInfo", pageInfo);
		return "Manager/TermManage";
	}
	@RequestMapping("/getAllLessionPlan")
	public String getAllLessionPlan(Model model) {
		model.addAttribute("acadamyList", acadamyService.selectAllAcadamy());
		model.addAttribute("lessionPlanlist", lessionPlanService.selectAllLessionPlan());
		return "Manager/LessionPlanManage";
	}
	
	@RequestMapping("/getAllCourse")
	public String getAllCourse(@RequestParam(defaultValue = "")String keyword,@RequestParam(defaultValue = "1") int pageNo,Model model) {
		model.addAttribute("acadamylist", acadamyService.selectAllAcadamy());
		PageHelper.startPage(pageNo, 8);
		List<Course> courselist = null;
		if(keyword.equals("")) {
			courselist = courseService.selectAllCourse();
		}else {
			courselist = courseService.selectCourseByKeyword(keyword);
		}
		
		PageInfo<Course> pageInfo = new PageInfo<>(courselist, 5);
		model.addAttribute("courselist", courselist);
		model.addAttribute("pageInfo", pageInfo);
		model.addAttribute("keyword", keyword);
		return "Manager/CourseManage";
	}
	
	@RequestMapping("/getAllStudent")
	public String getAllStudent(@RequestParam(defaultValue = "")String keyword,@RequestParam(defaultValue = "1") int pageNo,Model model) {
		List<Student> studentlist=null;
		PageHelper.startPage(pageNo, 8);
		
		if(keyword.equals("")) {
			studentlist = studentService.selectAllStudent();
		}
		else {
			 studentlist = studentService.selectStudentByKeyWord(keyword);
		}
		
		PageInfo<Student> pageInfo = new PageInfo<>(studentlist, 5);
		model.addAttribute("acadamylist", acadamyService.selectAllAcadamy());
		model.addAttribute("studentlist",studentlist);
		model.addAttribute("pageInfo", pageInfo);
		model.addAttribute("keyword", keyword);
		
		return "Manager/StudentManage";
	}
	@RequestMapping("/getAllStructure")
	public String getAllStructure(Model model) {
		model.addAttribute("structurelist",structureService.getAllStructure());
		model.addAttribute("acadamylist",acadamyService.selectAllAcadamy());
		model.addAttribute("termlist", termService.selectAllTerm());
		return "Manager/StructureManage";
	}
	@RequestMapping("/getCurrentTerm")
	public String getCurrentTerm(Model model) {
		model.addAttribute("currentterm", currentTermService.getAllCurrentTerm());
		return "Manager/CurrentTermManage";
	}
	@ResponseBody
	@GetMapping("/getMajorByacdmid")
	public List<Major> getMajorByacdmid(int acdmid) {
		List<Major> list = new ArrayList<>();
		list = majorService.selectMajorByacdmid(acdmid);
		return list;
	}
	@ResponseBody
	@GetMapping("/getBatchBymjid")
	public List<Batch> getBatchBymjid(int mjid) {
		List<Batch> list = new ArrayList<>();
		list = batchService.selectBatchByMjid(mjid);
		return list;
	}
	@ResponseBody
	@GetMapping("/getCourseByacdmid")
	public List<Course> getCourseByacdmid(int acdmid) {
		List<Course> list = new ArrayList<>();
		list = courseService.selectCourseByacdmid(acdmid);
		return list;
	}

//	@ResponseBody
//	@GetMapping("/getLessionPlanBymjandterm")
//	public LessionPlan getLessionPlanBymjandterm(int mjid,int termId,String cid){
//		LessionPlan lp = lessionPlanService.selectLessionPlanBymjidAndtermId(mjid, termId,cid);
//		return lp;
//	}
	@RequestMapping("/getAllTeacher")
	public String getAllTeacher(@RequestParam(defaultValue = "1") int pageNo ,Model model) {
		model.addAttribute("acadamylist",acadamyService.selectAllAcadamy());
		PageHelper.startPage(pageNo, 6);
		List<Teacher> teacherList = teacherService.selectAllTeacher();
		PageInfo<Teacher> pageInfo = new PageInfo<>(teacherList, 5);
		model.addAttribute("teacherlist",teacherList);
		model.addAttribute("pageInfo", pageInfo);
		return "Manager/TeacherManage";
	}
	@ResponseBody
	@GetMapping("/getStructureBycpid")
	public List<Structure> getStructureBycpid(String tid,int bid,int lpid){
		List<CoursePlan> cplist = coursePlanService.selectAllCoursePlan();
		List<Structure> list = new ArrayList<>();
		for(CoursePlan cp:cplist) {
//			System.out.println(cp);
			if(cp.getLessionPlan().getLpid()==lpid&&cp.getBatch().getBid()==bid) {
				int cpid = cp.getCpid();
				list = structureService.getStructureByCoursePlan(cpid);
			}
		}
		return list;
	}
	@ResponseBody
	@GetMapping("/getClassrmByBuildid")
	public List<Classroom> getClassrmByBuildid(int buildingId, String classrmtype){
		List<Classroom> classrmlist = classroomService.selectAllClassroom();
		List<Classroom> classrmlist2 = new ArrayList<Classroom>();
		for(Classroom c:classrmlist) {
			if(c.getClassrmtype().equals(classrmtype)&&c.getBuilding().getBuildingId()==buildingId) {
				classrmlist2.add(c);
			}
		}
		return classrmlist2;
	}
	
	
	//delete
	@ResponseBody
	@PostMapping("/deleteAcadamy")
	public void deleteAcadamy(int acdmid) {
		acadamyService.deleteAcadamyById(acdmid);
	}
	@ResponseBody
	@PostMapping("/deleteTerm")
	public void deleteTerm(int termId) {
		termService.deleteTermById(termId);
	}
	@ResponseBody
	@PostMapping("/deleteBatch")
	public void deleteBatch(int bid) {
		batchService.deleteBatchById(bid);
	}
	@ResponseBody
	@PostMapping("/deleteBuilding")
	public void deleteBuilding(int buildingId) {
		buildingService.deleteBuildingById(buildingId);
	}
	@ResponseBody
	@PostMapping("/deleteStudent")
	public void deleteStudent(String srollno) {
		usersSerivce.deleteUserById(srollno);
		studentService.deleteStudentById(srollno);
	}
	@ResponseBody
	@PostMapping("/deleteTeacher")
	public void deleteTeacher(String tid) {
		System.out.println(tid);
		teacherService.deleteTeacherById(tid);
		usersSerivce.deleteUserById(tid);
	}
	@ResponseBody
	@PostMapping("/deleteStructure")
	public void deleteStructure(int strid) {
		//System.out.println(strid);
		structureService.deleteStructureById(strid);
	}
	@ResponseBody
	@PostMapping("/deleteClassrm")
	public void deleteClassrm(int classrmid) {
		classroomService.deleteClassroomById(classrmid);
	}
	@ResponseBody
	@PostMapping("/deleteCoursePlan")
	public void deleteCoursePlan(int cpid) {
		coursePlanService.deleteCoursePlanById(cpid);
	}
	
	//modify
	@RequestMapping("/modifyAcadamy")
	public String modifyAcadamy(Acadamy a,Model model) {
		List<Acadamy> acdmlist = acadamyService.selectAllAcadamy();
		for(Acadamy acdm:acdmlist) {
			if(a.getAcdmname().equals(acdm.getAcdmname())) {
				model.addAttribute("name", a.getAcdmname());
				return "error/Exist";
			}
		}
		acadamyService.modifyAcadamy(a);
		return "redirect:/mc/getAllAcadamy";
	}
	@RequestMapping("/modifyMajor")
	public String modifyMajor(Major m,int acdmid,int termId,Model model) {
		List<Major> majorlist = majorService.selectAllMajor();
		for(Major mj:majorlist) {
			if(acdmid==mj.getAcadamy().getAcdmid()&&termId==mj.getTerm().getTermId()&&m.getMjname().equals(mj.getMjname())) {
				model.addAttribute("name",termService.selectTermById(termId).getTyear()+m.getMjname());
				return "error/Exist";
			}
		}
		majorService.modifyMajor(m, acdmid, termId);
		return "redirect:/mc/getAllMajor";
	}
	@RequestMapping("/modifyTerm")
	public String modifyTerm(Term t) {
		termService.modifyTerm(t);
		return "redirect:/mc/getAllTerm";
	}
	@RequestMapping("/modifyBatch")
	public String modifyBatch(Batch b,int mjid) {
		batchService.modifyBatch(b, mjid);
		return "redirect:/mc/getAllBatch";
	}
	@RequestMapping("/modifyBuilding")
	public String modifyBuilding(Building b,Model model) {
		List<Building> buildinglist = buildingService.selectAllBuilding();
		for(Building build:buildinglist) {
			if(b.getBuildname().equals(build.getBuildname())) {
				model.addAttribute("name", b.getBuildname());
				return "error/Exist";
			}
		}
		buildingService.modifyBuilding(b);
		return "redirect:/mc/getAllBuilding";
	}
	@RequestMapping("/modifyCourse")
	public String modifyCourse(Course c,int acdmid) {
		courseService.modifyCourse(c, acdmid);
		return "redirect:/mc/getAllCourse";
	}
	@RequestMapping("/modifyClassroom")
	public String modifyClassroom(Classroom c,int buildingId) {
		classroomService.modifyClassroom(c, buildingId);
		return "redirect:/mc/getAllClass";
	}
	@RequestMapping("/modifyStudent")
	public String modifyStudent(Student student,int bid) {
		studentService.modifyStudent(student, bid);
		return "redirect:/mc/getAllStudent";
	}
	@RequestMapping("/modifyTeacher")
	public String modifyTeacher(Teacher teacher,int acdmid) {
		teacherService.modifyTeacher(teacher, acdmid);
		return "redirect:/mc/getAllTeacher";
	}
	@RequestMapping("/modifyCurrentTerm/{termId}")
	public String modifyCurrentTerm(Model model,@PathVariable int termId) {
		currentTermService.modifyCurrentTerm(termId);
		model.addAttribute("msg", "修改成功！");
		return "redirect:/mc/getAllTerm";
	}
	
	
	
	//tomodify
	@GetMapping("/to_modifyMajor/{mjid}")
    public String tomodifyMajor(@PathVariable int mjid, Model model) {
		model.addAttribute("acadamylist", acadamyService.selectAllAcadamy());
        Major major = majorService.selectMajorById(mjid);
		model.addAttribute("major", major);
		List<Term> termlist = termService.selectAllTerm();
		List<Term> termlist2 = new ArrayList<>();
		for(Term t:termlist) {
			String tname = t.getTermName();
			if(tname.contains("秋")) {
				if(!(major.getTerm().getTyear()==t.getTyear())) {
				termlist2.add(t);
				}
			}
		}
		model.addAttribute("termlist2", termlist2);
		
        return "Manager/ModifyMajor";
    }
	@GetMapping("/to_modifyAcadamy/{acdmid}")
    public String tomodifyAcadamy(@PathVariable int acdmid, Model model) {
        model.addAttribute("acadamy", acadamyService.selectAcadamyById(acdmid));
        return "Manager/ModifyAcadamy";
    }
	@GetMapping("/to_modifyClass/{classrmid}")
    public String tomodifyClass(@PathVariable int classrmid, Model model) {
        model.addAttribute("classroom", classroomService.selectClassroomById(classrmid));
        return "Manager/ModifyClassroom";
    }
	@GetMapping("/to_modifyCourse/{cid}")
    public String tomodifyCourse(@PathVariable String cid, Model model) {
		model.addAttribute("acadamylist", acadamyService.selectAllAcadamy());
		model.addAttribute("course", courseService.selectCourseById(cid));
        return "Manager/ModifyCourse";
    }
	@GetMapping("/to_modifyTerm/{termId}")
    public String tomodifyTerm(@PathVariable int termId, Model model) {
        model.addAttribute("term", termService.selectTermById(termId));
        return "Manager/ModifyTerm";
    }
	@GetMapping("/to_modifyBatch/{bid}")
    public String tomodifyBatch(@PathVariable int bid,Model model) {
		model.addAttribute("acadamylist",acadamyService.selectAllAcadamy());
        model.addAttribute("batch",batchService.selectBatchById(bid));
        return "Manager/ModifyBatch";
    }
	@GetMapping("/to_modifyBuilding/{buildingId}")
    public String tomodifyBuilding(@PathVariable int buildingId, Model model) {
        model.addAttribute("building",buildingService.selectBuildingById(buildingId));
        return "Manager/ModifyBuilding";
    }
	@GetMapping("/to_modifyStudent/{srollno}")
    public String tomodifyStudent(@PathVariable String srollno, Model model) {
		model.addAttribute("acadamylist", acadamyService.selectAllAcadamy());
		model.addAttribute("student", studentService.selectStudentById(srollno));
        return "Manager/ModifyStudent";
    }
	@GetMapping("/to_modifyTeacher/{tid}")
	public String tomodifyTeacher(@PathVariable String tid, Model model) {
		Teacher teacher = teacherService.selectTeacherById(tid);
		model.addAttribute("teacher",teacher);
		List<Acadamy> acdmlist = acadamyService.selectAllAcadamy();
		List<Acadamy> acdmlist2 =new ArrayList<>();
		for(Acadamy a:acdmlist) {
			if(!(teacher.getAcadamy().getAcdmid()==a.getAcdmid())) {
				acdmlist2.add(a);
			}
		}
		model.addAttribute("acdmlist2", acdmlist2);
		return "Manager/ModifyTeacher";
	}
	
	
	//add
	@PostMapping("/addAcadamy")
	public String addAcadamy(Acadamy a,Model model) {
		List<Acadamy> acdmlist = acadamyService.selectAllAcadamy();
		for(Acadamy acdm:acdmlist) {
			if(a.getAcdmname().equals(acdm.getAcdmname())) {
				model.addAttribute("name", a.getAcdmname());
				return "error/Exist";
			}
		}
		acadamyService.addAcadamy(a);
		return "redirect:/mc/getAllAcadamy";
	}
	@RequestMapping("/addBatch")
	public String addBatch(Batch b,int mjid,Model model) {
		List<Batch> blist = batchService.selectAllBatch();
		for(Batch batch:blist) {
			if(mjid==batch.getMajor().getMjid()&&b.getBname().equals(batch.getBname())) {
				model.addAttribute("name", b.getBname());
				return "error/Exist";
			}
		}
		batchService.addBatch(b, mjid);
		return "redirect:/mc/getAllBatch";
	}
	@RequestMapping("/addBuilding")
	public String addBuilding(Building b,Model model) {
		List<Building> buildinglist = buildingService.selectAllBuilding();
		for(Building build:buildinglist) {
			if(b.getBuildname().equals(build.getBuildname())) {
				model.addAttribute("name", b.getBuildname());
				return "error/Exist";
			}
		}
		buildingService.addBuilding(b);
		return "redirect:/mc/getAllBuilding";
	}
	@RequestMapping("/addMajor")
	public String addMajor(Major m,int acdmid,int termId,Model model) {
		List<Major> majorlist = majorService.selectAllMajor();
		for(Major mj:majorlist) {
			if(acdmid==mj.getAcadamy().getAcdmid()&&termId==mj.getTerm().getTermId()&&m.getMjname().equals(mj.getMjname())) {
				model.addAttribute("name",termService.selectTermById(termId).getTyear()+m.getMjname());
				return "error/Exist";
			}
		}
		majorService.addMajor(m, acdmid, termId);
		return "redirect:/mc/getAllMajor";
	}
	@RequestMapping("/addClassrm")
	public String addClassrm(Classroom c,int buildingId,Model model) {
		List<Classroom> clrmlist = classroomService.selectAllClassroom();
		for(Classroom clrm:clrmlist) {
			if(buildingId==clrm.getBuilding().getBuildingId()&&c.getClrnum().equals(clrm.getClrnum())) {
				model.addAttribute("name",buildingService.selectBuildingById(buildingId).getBuildname()+c.getClrnum());
				return "error/Exist";
			}
		}
		classroomService.addClassroom(c, buildingId);
		return "redirect:/mc/getAllClass";
	}
	@RequestMapping("/addCourse")
	public String addCourse(Course course,int acdmid) {
		courseService.addCourse(course, acdmid);
		return "redirect:/mc/getAllCourse";
	}
	@RequestMapping("/addTerm")
	public String addTerm(Term term,Model model) {
		List<Term> tlist = termService.selectAllTerm();
		for(Term t:tlist) {
			if(term.getTyear()==t.getTyear()&&term.getTerm()==t.getTerm()) {
				model.addAttribute("name",t.getTermName());
				return "error/Exist";
			}
		}
		termService.addTerm(term);
		return "redirect:/mc/getAllTerm";
	}
	@RequestMapping("/addStudent")
	public String addStudent(Student student,int bid) {
		student.setImg("default.png");
		studentService.addStudent(student, bid);
		String pwd = student.getSrollno().substring(student.getSrollno().length()-6);
		Users user = new Users();
		
		user.setUid(student.getSrollno());
		user.setUpassword(pwdEncoder.encode(pwd));
		usersSerivce.addUser(user, 1);
		return "redirect:/mc/getAllStudent";
	}
	@ResponseBody
	@RequestMapping("/addStructure")
	public String addStructure (
			int cpid,
			int classrmid,
			@RequestParam("cycle") String[] cyc,
			@RequestParam("sweek") String[] week,
			@RequestParam("strtime") String[] stime
			) throws Exception{
		String msg="";
		CoursePlan cp = coursePlanService.selectCoursePlanById(cpid);
		int bid = cp.getBatch().getBid();
		String tid = cp.getTeacher().getTid();
		int termId = cp.getLessionPlan().getTerm().getTermId();
		int[] cycle = new int[cyc.length];
		int[] sweek = new int[week.length];
		int[] strtime = new int[stime.length];
		for(int i=0;i<cyc.length;i++) {
			cycle[i] = new Integer(cyc[i]);
		}
		for(int i=0;i<week.length;i++) {
			sweek[i] = new Integer(week[i]);
		}
		for(int i=0;i<stime.length;i++) {
			strtime[i] = new Integer(stime[i]);
		}
		
		List<Structure> strlist = structureService.getAllStructure();
		for(int i=0;i<cyc.length;i++) {//周次
			for(int j=0;j<week.length;j++) {//星期
				for(int k=0;k<stime.length;k++) {//节次
					for(Structure s:strlist) {
						if(s.getCoursePlan().getLessionPlan().getTerm().getTermId()==termId&&s.getCycle()==cycle[i]&&s.getSweek()==sweek[j]&&s.getStrtime()==strtime[k]&&s.getClassroom().getClassrmid()==classrmid) {
							return msg = "教室冲突！排课失败！";
							}
						if(s.getCoursePlan().getLessionPlan().getTerm().getTermId()==termId&&s.getCycle()==cycle[i]&&s.getSweek()==sweek[j]&&s.getStrtime()==strtime[k]&&s.getCoursePlan().getTeacher().getTid().trim().equals(tid)) {
							return msg = "教师冲突！排课失败！";
						}	
						if(s.getCoursePlan().getLessionPlan().getTerm().getTermId()==termId&&s.getCycle()==cycle[i]&&s.getSweek()==sweek[j]&&s.getStrtime()==strtime[k]&&s.getCoursePlan().getBatch().getBid()==bid) {
							return msg = "班级冲突！排课失败！";
						}
					}
					Structure s2 = new Structure(cycle[i], sweek[j],strtime[k]);
					structureService.addStructure(s2, cpid, classrmid);
					msg = "排课成功！请刷新页面查看！";
				}
			}
		}
		return msg;
	}
	@RequestMapping("/addTeacher")
	public String addTeacher(Teacher teacher,int acdmid) {
		String pwd = teacher.getTid().substring(teacher.getTid().length()-6);
		Users user = new Users();
		user.setUid(teacher.getTid());
		user.setUpassword(pwdEncoder.encode(pwd));
		usersSerivce.addUser(user, 2);
		teacherService.addTeacher(teacher, acdmid);
		return "redirect:/mc/getAllTeacher";
	}
	/*----------------------------------------------------------------------------------------*/
	/*----------------------------------------------------------------------------------------*/
	/*----------------------------------------------------------------------------------------*/
	@RequestMapping("/toInsertLessionPlan")
	public String insertLessionPlan(Model model) {
		List<Acadamy> acadamyList = acadamyService.selectAllAcadamy();
		model.addAttribute("acadamyList", acadamyList);
		return "Manager/LessionPlanManage";
	}


	@RequestMapping("/selectCourseByAcadamy")
	@ResponseBody
	public List<Course> getCourseByAcdamyId(int acdmid, Model model) {
		List<Course> list = new ArrayList<>();
		list = courseService.selectCourseByacdmid(acdmid);
		return list;
	}

	@RequestMapping("/selectTermBymjid")
	@ResponseBody
	public List<Term> getTermBytyear(int mjid, Model model) {
		int tyear;
		List<Term> termList = new ArrayList<>();
		Major major = new Major();
		major = majorService.selectMajorById(mjid);
		tyear = major.getTerm().getTyear();
		termList = termService.selectTermBytyear(tyear);
		return termList;
	}

	@PostMapping("/addLessionPlan")
	@ResponseBody
	public LessionPlan addLessionPlan(int mjid, int termId, String cid, Model model) {
		LessionPlan lessPlan1 = lessionPlanService.selectLessionPlanByMjidandTermIdandCid(mjid, termId, cid);
		if (lessPlan1 != null) {
			String str = "该课程已添加";
			model.addAttribute("msg", str);
			return null;
		} else {
			lessionPlanService.addLessionPlan(mjid, termId, cid);
			lessPlan1 = lessionPlanService.selectLessionPlanByMjidandTermIdandCid(mjid, termId, cid);
		}
		return lessPlan1;
	}

	@GetMapping("/selectLessionPlanBymjid")
	@ResponseBody
	public List<LessionPlan> selectLessionPlanBymjid(int mjid) {
		// System.out.println(mjid);
		// int majorId = new Integer(mjid);
		List<LessionPlan> list = new ArrayList<>();
		list = lessionPlanService.selectLessionPlanBymjid(mjid);
		return list;
	}

	@GetMapping("/deleteLessionPlanBylpid")
	@ResponseBody
	public void deleteLessionPlanBylpid(int lpid) {
		lessionPlanService.deleteLessionPlanById(lpid);
	}

	@GetMapping("/toCoursePlan")
	public String toCoursePlan(int lpid, int mjid,Model model) {
		LessionPlan lessionPlan = lessionPlanService.selectLessionPlanById(lpid);
		model.addAttribute("lessionPlan", lessionPlan);
		List<Acadamy> acadamyList = acadamyService.selectAllAcadamy();
		model.addAttribute("acadamyList", acadamyList);
		List<Batch> batchList = batchService.selectBatchByMjid(mjid);
		model.addAttribute("batchList", batchList);
		return "Manager/CoursePlanManage";
	}
	
	@GetMapping("/toAddStructure")
	public String toAddStructure(@RequestParam(defaultValue = "1") int pageNo ,int cpid, int bid,Model model) {
		List<Building> buildlist = buildingService.selectAllBuilding();
		model.addAttribute("buildlist", buildlist);
		CoursePlan coursePlan = coursePlanService.selectCoursePlanById(cpid);
		model.addAttribute("coursePlan", coursePlan);
		model.addAttribute("classrmtype",coursePlan.getclassrmtype());
		PageHelper.startPage(pageNo, 8);
		List<Structure> strlist = structureService.getStructureByCoursePlan(cpid);
		PageInfo<Structure> pageInfo = new PageInfo<>(strlist, 5);
		model.addAttribute("strlist", strlist);
		model.addAttribute("pageInfo", pageInfo);
		model.addAttribute("cpid", cpid);
		model.addAttribute("bid", bid);
		return "Manager/AddStructure";
	}

	@GetMapping("/getTeacherByAcdmid")
	@ResponseBody
	public List<Teacher> getTeacherByAcdmid(int acdmid) {
		List<Teacher> list = teacherService.selectTeacherByAcdmid(acdmid);
		return list;
	}
	
	@GetMapping("/getCoursePlan")
	@ResponseBody
	public List<CoursePlan> getCoursePlan(int mjid, int termId, Model model) {
		List<CoursePlan> cplist = coursePlanService.selectAllCoursePlan();
		List<CoursePlan> cplist2 = new ArrayList<>();
		for(CoursePlan cp:cplist) {
			if(cp.getLessionPlan().getMajor().getMjid()==mjid&&cp.getLessionPlan().getTerm().getTermId()==termId) {
				cplist2.add(cp);
			}
		}
		return cplist2;
	}
	
	@PostMapping("/addOneCoursePlan")
	@ResponseBody
	public String addOne(int tid, int bid, int lpid, char classrmtype, Model model) {
		CoursePlan coursePlan = coursePlanService.selectCoursePlanByThreeId(bid, lpid);
		String str = "";
		if (coursePlan != null) {
			str = "该开课计划已存在";
			// model.addAttribute("msg", str);
		} else {
//			System.out.println(classrmtype);
			coursePlanService.addCoursePlan(tid, bid, classrmtype, lpid);
			str = "开课计划添加成功！";
		}
		return str;
	}

	@GetMapping("/toSchedule")
	public String toSchedule() {
		return "Manager/Schedule";
	}

	@GetMapping("/getScheduleByBidAndCycle")
	@ResponseBody
	public List<Structure> getScheduleByBidAndCycle(int bid, int cycle, Model model) {
		List<CoursePlan> coursePlanList = coursePlanService.selectCoursePlanByBid(bid);
		List<Structure> list = new ArrayList<>();
		for (CoursePlan coursePlan : coursePlanList) {
			List<Structure> strucList = structureService.getStructureByCoursePlanAndCycle(coursePlan.getCpid(), cycle);
			for (Structure struct : strucList) {
				list.add(struct);
			}
		}
		return list;
	}
	
	
	
	
	@GetMapping("/downloadStudentMould")
	public ResponseEntity<byte[]> downloadStudentMould() throws IOException {
		String realPath = ResourceUtils.getURL("classpath:files/studentMould.xlsx").getPath();
		byte[] data = FileUtils.readFileToByteArray(new File(realPath));
		HttpHeaders headers = new HttpHeaders();
		headers.add("content-disposition", "attachment;filename=studentMould.xlsx");
		ResponseEntity<byte[]> responseEntity = new ResponseEntity<byte[]>(data, headers, HttpStatus.OK);
		return responseEntity;

	}
	
	
	@PostMapping("/uploadStudent")
	public String uploadStudent(MultipartFile file, Model model) throws Exception {
		boolean flag = studentService.uploadStudent(file);
		return "redirect:/mc/getAllStudent";
	}
	
	@PostMapping("uploadCourse")
	public String uploadCourse(MultipartFile file, Model model) throws Exception {
		boolean flag = courseService.uploadCourse(file);
		return "redirect:/mc/getAllCourse";
	}
	
	@GetMapping("/downloadCourseMould")
	public ResponseEntity<byte[]> downloadCourseMould() throws IOException {

		String realPath = ResourceUtils.getURL("classpath:files/courseMould.xlsx").getPath();
		byte[] data = FileUtils.readFileToByteArray(new File(realPath));
		HttpHeaders headers = new HttpHeaders();
		headers.add("content-disposition", "attachment;filename=courseMould.xlsx");
		ResponseEntity<byte[]> responseEntity = new ResponseEntity<byte[]>(data, headers, HttpStatus.OK);
		return responseEntity;

	}
	
	/*
	 * @GetMapping("/selectStudentByKeyword")
	 * 
	 * @ResponseBody public List<Student> getStudentByKeyword(String
	 * keyword,@RequestParam(defaultValue="1") int pageNo, Model model){
	 * PageHelper.startPage(pageNo, 1);
	 * 
	 * List<Student> list = studentService.selectStudentByKeyWord(keyword);
	 * 
	 * model.addAttribute("stuList", list); model.addAttribute("keyword", keyword);
	 * PageInfo<Student> pageInfo=new PageInfo<>(list,5);
	 * //将PageInfo对象添加到Model，这样可以在页面上获取所有的分页信息
	 * model.addAttribute("pageInfo",pageInfo); return list; }
	 */
	
	/*
	 * @GetMapping("/selectCourseByKeyword")
	 * 
	 * @ResponseBody public List<Course> getCourseByKeyword(String
	 * keyword,@RequestParam(defaultValue="1") int pageNo,Model model){
	 * 
	 * List<Course> list = courseService.selectCourseByKeyword(keyword); return
	 * list; }
	 */
	
	@GetMapping("/deleteCourseByCid")
	@ResponseBody
	public void deleteCourseByCid(String cid) {
		courseService.deleteCourseById(cid);
	}
}
	
	
	
	

